from rest_framework.viewsets import ModelViewSet
from django.contrib.auth.models import Group

from users.models import User
from meiduo_admin.serializers.adminManageSerializer import AdminManageSerializer
from meiduo_admin.utils import PaginatorShow
from meiduo_admin.serializers.userGroupSerializer import UserGroupSerializer
from meiduo_admin.permissions import *


class AdminManageView(ModelViewSet):
    queryset = User.objects.filter(is_staff=True).order_by("id")
    serializer_class = AdminManageSerializer
    permission_classes = [IsSuperUser]

    pagination_class = PaginatorShow

    def get_serializer(self, *args, **kwargs):
        if args:
            if not isinstance(args[0],User):
                for user in args[0]:
                    user.mobile = user.phone_number
            else:
                user = args[0]
                user.mobile = user.phone_number
                kwargs["data"]["phone_number"] = kwargs["data"].pop("mobile")
        else:
            kwargs["data"]["phone_number"] = kwargs["data"].pop("mobile")
        return super().get_serializer(*args,**kwargs)


class AdminPermissionSimpleView(ModelViewSet):
    queryset = Group.objects.all()
    serializer_class = UserGroupSerializer
    permission_classes = [IsAdminUser]